﻿using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using System;

namespace HeritageAcademy.Library.Controls
{
	/// <summary>
	/// Table row class.
	/// </summary>
	public partial class StatTableRow
    {
        #region Ctor
        public StatTableRow()
		{
			InitializeComponent();
		}
        #endregion

        #region Properties
        /// <summary>
        /// Course chapter.
        /// </summary>
        public string Chapter
        {
            get { return lblChapter.Content.ToString(); }
            set { lblChapter.Content = value; }
        }

        /// <summary>
        /// Number of right answers in chapter.
        /// </summary>
        public string QuestionsActual
        {
            get { return lblQuestionsActual.Content.ToString(); }
            set 
            {
                if (value == lblQuestionsActual.Content.ToString()) return;
                lblQuestionsActual.Content = value;
                UpdateDetails(1);
            }
        }

        /// <summary>
        /// Overall number of questions in chapter.
        /// </summary>
        public string QuestionAll
        {
            get { return lblQuestionsAll.Content.ToString(); }
            set 
            {
                if (value == lblQuestionsAll.Content.ToString()) return;
                lblQuestionsAll.Content = value;
                UpdateDetails(1);
            }
        }

        /// <summary>
        /// Number of executed tasks in chapter.
        /// </summary>
        public string TasksActual
        {
            get { return lblTasksActual.Content.ToString(); }
            set 
            {
                if (value == lblTasksActual.Content.ToString()) return;
                lblTasksActual.Content = value;
                UpdateDetails(2);
            }
        }

        /// <summary>
        /// Overall number of tasks in chapter.
        /// </summary>
        public string TasksAll
        {
            get { return lblTasksAll.Content.ToString(); }
            set 
            {
                if (value == lblTasksAll.Content.ToString()) return;
                lblTasksAll.Content = value;
                UpdateDetails(2);
            }
        }

	    /// <summary>
	    /// Percent of material mastering.
	    /// </summary>
	    public double BarPercent { get; private set; }

	    /// <summary>
        /// Update visuals with new values.
        /// </summary>
        private void UpdateDetails(int task)
        {
            switch (task)
            {   
                case 1:
                    if (QuestionAll == QuestionsActual)
                        lblQuestionsActual.Visibility = Visibility.Collapsed;
                    CalculatePercent();
                    break;
                case 2:
                    if (TasksAll == TasksActual)
                        lblTasksActual.Visibility = Visibility.Collapsed;
                    CalculatePercent();
                    break;
                case 3:
                    bdBarPercent.Width = BarPercent * bdBarHolder.ActualWidth;
                    var b1 = (byte)(255 - (BarPercent * 210));
                    bdBarPercent.Background = new SolidColorBrush(Color.FromArgb(255, 255, b1, 0));
                    break;
            }
        }

        /// <summary>
        /// Calculate percent of completed tasks and answered questions (as a sum).
        /// </summary>
        private void CalculatePercent()
        {
            try
            {
                // avoid double operations
                if (!QuestionAll.Equals("0") & !QuestionsActual.Equals("0") &
                    !TasksAll.Equals("0") & !TasksActual.Equals("0"))
                {
                    // calculate percent
                    BarPercent = (Convert.ToInt32(QuestionsActual) * 0.5) / (Convert.ToInt32(QuestionAll))
                                    + (Convert.ToInt32(TasksActual) * 0.5) / (Convert.ToInt32(TasksAll));
                    // if get a wrong value(s) set the maximum value of bar percent
                    if (BarPercent > 1)
                        BarPercent = 1;
                    // update bar
                    UpdateDetails(3);
                    // update tooltip
                    bdBarHolder.ToolTip = "Выполнено " + Math.Round(BarPercent * 100) + " % заданий";
                }
            }
            catch (Exception)
            {
                throw new DivideByZeroException();
            }
            
        }
        #endregion

        #region Handlers
        /// <summary>
        /// Change bar size when bar holder size changed.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Size changed event arguments</param>
        private void bdBarHolder_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (!double.IsInfinity(BarPercent))
                bdBarPercent.Width = BarPercent*((Border) sender).ActualWidth;
        }
        #endregion
    }
}